<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar title="设置密码" left-arrow @click-left="onClickLeft"/>
    <van-form @submit="onSubmit">
      <van-field v-model="user.username" disabled label="学号"/>
      <van-field v-model="user.password" type="password" label="原密码" placeholder="填写原密码"/>
      <van-field v-model="user.newPassword" type="password" label="新密码" placeholder="填写新密码"/>
      <van-field v-model="user.newPassword2" type="password" label="确认密码" placeholder="再次填写确认密码"/>
      <div style="margin: 16px;">
        <van-button round block type="info" native-type="submit" color="#1baeae">提交</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {getInfo, modifyPassword} from "@/api/user";
import { Notify } from 'vant';

export default {
  name: "index",
  data(){
    return{
      user:{
        id:'',
        username:'',
        password:'',
        newPassword:'',
        newPassword2:''
      },
    }
  },
  created() {
    this.getUserInfo();
  },
  methods:{
    /**
     * 返回设置页面
     * */
    onClickLeft(){
      this.$router.push("/user/setting")
    },
    /**
     * 提交请求
     * */
    async onSubmit(values) {
      // 校验密码
      if (this.user.password === ''){
        Notify({ type: 'danger', message: '原密码不能为空!' });
        return false;
      }
      // 校验密码
      if (this.user.newPassword === ''){
        Notify({ type: 'danger', message: '新密码不能为空!' });
        return false;
      }
      // 校验密码
      if (this.user.newPassword2 === ''){
        Notify({ type: 'danger', message: '确认密码不能为空!' });
        return false;
      }
      // 校验密码
      if (this.user.newPassword !== this.user.newPassword2){
        Notify({ type: 'danger', message: '两次密码不一致!' });
        return false;
      }
      // 发送修改密码请求
      let res = await modifyPassword(this.user);
      Notify({ type: 'success', message: res.message });
      this.onClickLeft();
    },
    /**
     * 获取用户信息
     * */
    async getUserInfo() {
     let res =  await getInfo();
     this.user.username = res.data.username
     this.user.id = res.data.id
    }
  }
}
</script>

<style scoped lang="scss">

</style>
